﻿using System;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using Hutech.Authentication.Library.Business;
using System.Collections.Generic;

namespace Hutech.Authentication.Library.Business.WebHelpers
{
    public static class WebUser
    {
        public static bool IsInRole(string role)
        {
            if (HttpContext.Current == null || HttpContext.Current.User == null){return false;}
            if (role == null){return false;}
            if (role.Contains("All Users")){return true;}
            if (!HttpContext.Current.Request.IsAuthenticated){return false;}
            if (HttpContext.Current.User.IsInRole("Admins")){return true;}
            if (String.IsNullOrEmpty(role)){return false;}
            return HttpContext.Current.User.IsInRole(role);
        }


        public static bool IsInRoles(string roles)
        {
            if (IsInRole("Admins"))
                return true;
            if (String.IsNullOrEmpty(roles))
                return false;
            if (roles.Contains("All Users;"))
                return true;
            if (!HttpContext.Current.Request.IsAuthenticated)
                return false;

            foreach (string role in roles.Split(new char[] { ';' }))
            {
                if (role.IndexOf("All Users") > -1)
                    return true;
                if (IsInRole(role))
                    return true;
            }
            return false;
        }


        public static bool IsInRoles(IList roles)
        {
            if (IsInRole("Admins"))
                return true;

            if (roles == null)
                return false;

            if (roles.Contains("All Users"))
                return true;

            if (!HttpContext.Current.Request.IsAuthenticated)
                return false;

            foreach (string role in roles)
            {
                if (role.Contains("All Users"))
                    return true;
                if (IsInRole(role))
                    return true;
            }
            return false;
        }

        #region thiep lap quyen 
        public static bool IsAdmin
        {
            get
            {
                if (!HttpContext.Current.Request.IsAuthenticated)
                    return false;
                return IsInRole("Admins");
            }
        }
        public static bool IsChuaXacDinh
        {
            get
            {
                if (!HttpContext.Current.Request.IsAuthenticated)
                    return false;
                return IsInRole("ChuaXacDinh");
            }
        }
        #endregion

        public static bool IsContentAdmin
        {
            get
            {
                if (!HttpContext.Current.Request.IsAuthenticated)
                    return false;
                return IsInRole("Content Admins");
            }
        }

        public static bool IsRoleAdmin
        {
            get
            {
                if (!HttpContext.Current.Request.IsAuthenticated)
                    return false;
                return IsInRole("Role Admins");
            }
        }

        public static bool IsAdminOrContentAdmin
        {
            get
            {
                return IsAdmin || IsContentAdmin;
            }
        }

        public static bool IsAdminOrRoleAdmin
        {
            get
            {
                return IsAdmin || IsRoleAdmin;
            }
        }

        /// <summary>
        /// Láº¥y userId cá»§a User Ä‘ang Ä‘Äƒng nháº­p
        /// KhÃ¡nh thÃªm vÃ o 04-05-2011
        /// </summary>
        /// <returns></returns>
        public static Guid GetCurrentUserId()
        {
            if (HttpContext.Current == null || HttpContext.Current.User == null)
                return Guid.Empty;
            if (!HttpContext.Current.Request.IsAuthenticated)
                return Guid.Empty;
            if (HttpContext.Current.Session["LoggedUserId"] != null)
            {
                return new Guid(HttpContext.Current.Session["LoggedUserId"].ToString());
            }
            else
            {
                SiteUser ur = new SiteUser(HttpContext.Current.User.Identity.Name);
                return ur.UserId;
            }
        }

        #region Add by tsandtm 20/9/2012
        /// <summary>
        /// Check permission by actioncommand. define by deverloper
        /// </summary>
        /// <param name="ActionName"></param>
        /// <returns>true of false</returns>
        public static bool isPermission(string ActionCommand)
        {
            Guid UserID=WebUser.GetCurrentUserId();
            if (UserPermission.CheckPermission(UserID, ActionCommand)) return true;

            List<UserRoles> lUserRoles = UserRoles.GetAllByUserID(UserID);
            foreach (UserRoles item in lUserRoles)
                if (RolePermission.CheckPermission(item.RoleId, ActionCommand)) return true;
            
            return false;
        }
        #endregion

      
    }
}
